<template>
  <el-dialog :title="form.id ? '编辑' : '林权变更登记申请'" v-model="visible"
             :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef v-loading="loading" label-width="120" >
      <el-row :gutter="24">
        <el-col :span="24" class="mb20">
          <el-form-item label="登记类型" prop="djlx">
            <el-radio-group v-model="form.djlx">
              <el-radio :label="item.value" v-for="(item, index) in change_register_type" border :key="index">
                {{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="受理编号" prop="slbh">
            <el-input v-model="form.slbh" placeholder="请输入受理编号"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-tabs class="demo-tabs" type="border-card" v-model="activeName">
        <el-tab-pane label="基础信息" name="first">
          <template #label>
             <span class="label-title" >基础信息</span> 
          </template>
          <el-row :gutter="24">
          <el-col :span="24" class="mb20">
            <el-form-item label="不动产权证号" prop="bdcqzh">
              <el-input v-model="form.bdcqzh" placeholder="请输入不动产权证号"/>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item label="宗地代码" prop="zddm">
              <el-input v-model="form.zddm" placeholder="请输入宗地代码"/>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item label="变更事由" prop="bgsy">
              <el-radio-group v-model="form.bgsy">
                <el-radio :label="item.value" v-for="(item, index) in change_because" :key="index">{{
                    item.label
                  }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        </el-tab-pane>
        <el-tab-pane label="权所属变更明细" name="second">
          <el-row :gutter="24">
          <el-col :span="12" class="mb20">
            <el-form-item label="原权利人" prop="yqlr" label-width="140" >
              <el-input v-model="form.yqlr" placeholder="请输入原权利人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="原权利人证件类型" prop="yqlrzjlx" label-width="140">
              <el-select v-model="form.yqlrzjlx" placeholder="请选择原权利人证件类型">
                <el-option :value="item.value" :label="item.label" v-for="(item, index) in idcard_type"
                           :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item label="原权利人证件号码" prop="yqlrzjhm" label-width="140">
              <el-input v-model="form.yqlrzjhm" placeholder="请输入原权利人证件号码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="现权利人" prop="xqlr" label-width="140">
              <el-input v-model="form.xqlr" placeholder="请输入现权利人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="现权利人证件类型" prop="xqlrzjlx" label-width="140">
              <el-select v-model="form.xqlrzjlx" placeholder="请选择现权利人证件类型">
                <el-option :value="item.value" :label="item.label" v-for="(item, index) in idcard_type"
                           :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item label="现证件人证件号码" prop="xqlrzjhm" label-width="140">
              <el-input v-model="form.xqlrzjhm" placeholder="请输入现证件人证件号码"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12" class="mb20">
            <el-form-item label="原面积" prop="ymj" label-width="140">
              <el-input v-model="form.ymj" placeholder="请输入原面积"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="现面积" prop="xmj" label-width="140">
              <el-input v-model="form.xmj" placeholder="请输入现面积"/>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item label="林种调整类型" prop="lzdzlx" label-width="140">
              <el-radio-group v-model="form.lzdzlx">
                <el-radio :label="item.value" v-for="(item, index) in forest_category_change" :key="index">
                  {{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        </el-tab-pane>
        <el-tab-pane label="材料清单（原件核验）" name="third">
          <template #label>
             <span class="label-title" >材料清单（原件核验）</span> 
          </template>
          <el-row :gutter="24">
          <el-col :span="24" class="mb20">
            <p  style="text-indent:3em;color:black">材料清单里需要包含<span style="color:red">(分多次上传)：</span></p>
            <p style="text-indent:3em">1、不动产权证书原件</p>
            <p style="text-indent:3em">2、集体林权流转合同<span style="color:red">（需载明林木权属处置条款）</span> </p>
            <p style="text-indent:3em">3、村民会议三分之二同意书<span style="color:red">（集体林地流转时提供）</span></p>
            <p style="text-indent:3em">4、继承公证书/司法文书<span style="color:red">（继承或司法裁定时必需）</span></p>
            <p style="text-indent:3em;margin-bottom:10px">5、抵押权人同意书<span style="color:red">（已抵押林权时需提供）</span></p>
            <el-form-item label="材料清单" prop="clqd">
              <upload-file v-model="form.clqd"></upload-file>
            </el-form-item>
          </el-col>
        </el-row>
        </el-tab-pane>
      </el-tabs>
      <!-- <el-card class="box-card" shadow="always">
        <div slot="header"><span>一、基础信息</span></div>
        <el-row :gutter="24">
          <el-col :span="24" class="mb20">
            <el-form-item label="不动产权证号" prop="bdcqzh">
              <el-input v-model="form.bdcqzh" placeholder="请输入不动产权证号"/>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item label="宗地代码" prop="zddm">
              <el-input v-model="form.zddm" placeholder="请输入宗地代码"/>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item label="变更事由" prop="bgsy">
              <el-radio-group v-model="form.bgsy">
                <el-radio :label="item.value" v-for="(item, index) in change_because" border :key="index">{{
                    item.label
                  }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card> -->

      <!-- <el-card class="box-card" shadow="always">
        <div slot="header"><span>二、权所属变更明细</span></div>
        <div slot="header"><span>1、权利人信息变更</span></div>
        <el-row :gutter="24">
          <el-col :span="12" class="mb20">
            <el-form-item label="原权利人" prop="yqlr">
              <el-input v-model="form.yqlr" placeholder="请输入原权利人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="原权利人证件类型" prop="yqlrzjlx">
              <el-select v-model="form.yqlrzjlx" placeholder="请选择原权利人证件类型">
                <el-option :value="item.value" :label="item.label" v-for="(item, index) in idcard_type"
                           :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item label="原权利人证件号码" prop="yqlrzjhm">
              <el-input v-model="form.yqlrzjhm" placeholder="请输入原权利人证件号码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="现权利人" prop="xqlr">
              <el-input v-model="form.xqlr" placeholder="请输入现权利人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="现权利人证件类型" prop="xqlrzjlx">
              <el-select v-model="form.xqlrzjlx" placeholder="请选择现权利人证件类型">
                <el-option :value="item.value" :label="item.label" v-for="(item, index) in idcard_type"
                           :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item label="现证件人证件号码" prop="xqlrzjhm">
              <el-input v-model="form.xqlrzjhm" placeholder="请输入现证件人证件号码"/>
            </el-form-item>
          </el-col>
        </el-row>
        <div slot="header"><span>2、林地属性变更</span></div>
        <el-row :gutter="24">
          <el-col :span="12" class="mb20">
            <el-form-item label="原面积" prop="ymj">
              <el-input v-model="form.ymj" placeholder="请输入原面积"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="现面积" prop="xmj">
              <el-input v-model="form.xmj" placeholder="请输入现面积"/>
            </el-form-item>
          </el-col>
          <el-col :span="24" class="mb20">
            <el-form-item label="林种调整类型" prop="lzdzlx">
              <el-radio-group v-model="form.lzdzlx">
                <el-radio :label="item.value" v-for="(item, index) in forest_category_change" border :key="index">
                  {{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
      <el-card class="box-card" shadow="always">
        <div slot="header"><span>三、材料清单（原件核验）</span></div>
        <el-row :gutter="24">
          <el-col :span="24" class="mb20">
            <el-form-item label="材料清单" prop="clqd">
              <upload-file v-model="form.clqd"></upload-file>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card> -->
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="ChangeForestAppleRegisterDialog">
// ========== 1. 导入语句 ==========
import {useDict} from '/@/hooks/dict';
import {rule} from '/@/utils/validate';
import {useMessage} from "/@/hooks/message";
import {getObj, addObj, putObj, validateExist} from '/@/api/forestry/changeForestAppleRegister';

// ========== 2. 组件定义 ==========
// 定义组件事件
const emit = defineEmits(['refresh']);
const activeName = ref('first');
// ========== 3. 响应式数据定义 ==========
// 基础响应式变量
const dataFormRef = ref(); // 表单引用
const visible = ref(false); // 弹窗显示状态
const loading = ref(false); // 加载状态

// 表单数据对象
const form = reactive({
  id: '', // 主键
  djlx: '', // 登记类型
  slbh: '', // 受理编号
  bdcqzh: '', // 不动产权证号
  zddm: '', // 宗地代码
  bgsy: '', // 变更事由
  yqlr: '', // 原权利人
  yqlrzjlx: '', // 原权利人证件类型
  yqlrzjhm: '', // 原权利人证件号码
  xqlr: '', // 现权利人
  xqlrzjlx: '', // 现权利人证件类型
  xqlrzjhm: '', // 现证件人证件号码
  ymj: '', // 原面积
  xmj: '', // 现面积
  lzdzlx: '', // 林种调整类型
  clqd: '', // 材料清单
});

// ========== 4. 字典数据处理 ==========
// 加载字典数据
const {
  forest_category_change,
  change_because,
  idcard_type,
  change_register_type
} = useDict('forest_category_change', 'change_because', 'idcard_type', 'change_register_type');

// ========== 5. 表单校验规则 ==========
const dataRules = ref({
  djlx: [
    {required: true, message: '登记类型不能为空', trigger: 'blur'}
  ],
  slbh: [
    {required: true, message: '受理编号不能为空', trigger: 'blur'}
  ],
  bdcqzh: [
    {required: true, message: '不动产权证号不能为空', trigger: 'blur'}
  ],
  zddm: [
    {required: true, message: '宗地代码不能为空', trigger: 'blur'}
  ],
  bgsy: [
    {required: true, message: '变更事由不能为空', trigger: 'blur'}
  ],
  clqd: [
    {required: true, message: '材料清单不能为空', trigger: 'blur'}
  ],
});

// ========== 6. 方法定义 ==========
// 获取详情数据
const getChangeForestAppleRegisterData = async (id: string) => {
  try {
    loading.value = true;
    const {data} = await getObj({id: id});
    // 直接将第一条数据赋值给表单
    Object.assign(form, data[0]);
  } catch (error) {
    useMessage().error('获取数据失败');
  } finally {
    loading.value = false;
  }
};

// 打开弹窗方法
const openDialog = (id: string) => {
  visible.value = true;
  form.id = '';

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取ChangeForestAppleRegister信息
  if (id) {
    form.id = id;
    getChangeForestAppleRegisterData(id);
  }
};

// 提交表单方法
const onSubmit = async () => {
  loading.value = true; // 防止重复提交

  // 表单校验
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) {
    loading.value = false;
    useMessage().error('还有信息未填写完毕');
    return false;
  }

  try {
    // 根据是否有ID判断是新增还是修改
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh'); // 通知父组件刷新列表
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// ========== 7. 对外暴露 ==========
// 暴露方法给父组件
defineExpose({
  openDialog
});
</script> 
<style scoped lang="scss"> 
.label-title::before{
  content: "*";
  color: var(--el-color-danger);
  margin-right: 4px;
}
</style>